﻿using DocumentFormat.OpenXml.Wordprocessing;
using System.Collections.Generic;

namespace Tatibah
{
    /// <summary>
    /// This class represents a Table within a Document
    /// </summary>
    public class Table : iDocumentAppend, iCellAppend
    {
        /// <summary>
        /// When set to fixed just set the Columns widths
        /// </summary>
        public TableLayoutValues TableLayout = TableLayoutValues.Autofit;

        /// <summary>
        /// The border style
        /// </summary>
        public BorderValues TopBorder = BorderValues.Single;
        
        /// <summary>
        /// The border style
        /// </summary>public BorderValues BottomBorder = BorderValues.Single;
        public BorderValues BottomBorder = BorderValues.Single;

        /// <summary>
        /// The border style
        /// </summary>public BorderValues BottomBorder = BorderValues.Single;
        public BorderValues LeftBorder = BorderValues.Single;

        /// <summary>
        /// The border style
        /// </summary>public BorderValues BottomBorder = BorderValues.Single;
        public BorderValues RightBorder = BorderValues.Single;

        /// <summary>
        /// The border style
        /// </summary>public BorderValues BottomBorder = BorderValues.Single;
        public BorderValues InsideHorizontalBorder = BorderValues.Single;
        
        /// <summary>
        /// The border style
        /// </summary>public BorderValues BottomBorder = BorderValues.Single;
        public BorderValues InsideVerticalBorder = BorderValues.Single;

        /// <summary>
        /// Border width in DXAs
        /// </summary>
        public uint BottomBorderWidth = 0;

        /// <summary>
        /// Border width in DXAs
        /// </summary>
        public uint LeftBorderWidth = 0;

        /// <summary>
        /// Border width in DXAs
        /// </summary>
        public uint RightBorderWidth = 0;

        /// <summary>
        /// Border width in DXAs
        /// </summary>
        public uint TopBorderWidth = 0;

        /// <summary>
        /// Border width in DXAs
        /// </summary>
        public uint InsideHorizontalBorderWidth = 0;

        /// <summary>
        /// Border width in DXAs
        /// </summary>
        public uint InsideVerticalBorderWidth = 0;

        /// <summary>
        /// Border colour as hex string RRGGBB
        /// </summary>
        public string  BottomBorderColour = "";

        /// <summary>
        /// Border colour as hex string RRGGBB
        /// </summary>
        public string LeftBorderColour = "";

        /// <summary>
        /// Border colour as hex string RRGGBB
        /// </summary>
        public string RightBorderColour = "";

        /// <summary>
        /// Border colour as hex string RRGGBB
        /// </summary>
        public string TopBorderColour = "";

        /// <summary>
        /// Border colour as hex string RRGGBB
        /// </summary>
        public string InsideHorizontalBorderColour = "";

        /// <summary>
        /// Border colour as hex string RRGGBB
        /// </summary>
        public string InsideVerticalBorderColour = "";


        /// <summary>
        /// Set the Alignment of the Table
        /// </summary>
        public TableRowAlignmentValues Alignment = TableRowAlignmentValues.Left;

        /// <summary>
        /// If documentSetting width to percent units are fifieth of 1% other wise units are EMUs. Default width is 100%
        /// </summary>
        public double Width = 0;

        /// <summary>
        /// The type of units used 
        /// </summary>
        public TableWidthUnitValues WidthType = TableWidthUnitValues.Pct; // Default is percent

        /// <summary>
        /// Left Indentation of the table, units are DXA
        /// </summary>
        public int LeftIndentation = 0;

        /// <summary>
        /// The type of units used 
        /// </summary>
        public TableWidthUnitValues LeftIndentationUnits = TableWidthUnitValues.Pct; // Default is percent

        /// <summary>
        /// The Top margin of all Cells, units are DXA
        /// </summary>
        public int CellTopMargin = -1;


        /// <summary>
        /// The type of units used 
        /// </summary>
        public TableWidthUnitValues CellTopMarginUnits = TableWidthUnitValues.Dxa; // Default is Dxa
        
        /// <summary>
        /// Margin in DXA units
        /// </summary>
        public int CellBottomMargin = -1;

        /// <summary>
        /// The type of units used 
        /// </summary>
        public TableWidthUnitValues CellBottomMarginUnits = TableWidthUnitValues.Dxa; // Default is Dxa

        /// <summary>
        /// Margin in DXA units
        /// </summary>
        public int CellLeftMargin = -1;

        /// <summary>
        /// The type of units used 
        /// </summary>
        public TableWidthUnitValues CellLeftMarginUnits = TableWidthUnitValues.Dxa; // Default is Dxa

        /// <summary>
        /// Margin in DXA units
        /// </summary>
        public int CellRightMargin = -1;

        /// <summary>
        /// The type of units used 
        /// </summary>
        public TableWidthUnitValues CellRightMarginUnits = TableWidthUnitValues.Dxa; // Default is Dxa
      
        private List<Row> rows = new List<Row>();
        private List<Column> columns = new List<Column>();
     
        private int numRows = 0;
        private int numCells = 0;

        /// <summary>
        /// Create an Empty Table from a number of rows and columns
        /// </summary>
        /// <param name="Rows"></param>
        /// <param name="Cells"></param>
        public Table(int Rows, int Cells)
        {
            for (int row = 0; row < Rows; row++)
            {
                Row newRow = new Row(Cells, this);
                rows.Add(newRow);
            }
            numRows = Rows;

            for (int cell = 0; cell < Cells; cell++)
            {
                Column col = new Column();
                columns.Add(col);
            }
            numCells = Cells;
            
        }

        #region P R O P E R T I E S

        /// <summary>
        /// Get all the Rows from the Table
        /// </summary>
        public List<Row> Rows
        {
            get { return rows; }
        }

        /// <summary>
        /// Get the List of Column Properties from the Table
        /// </summary>
        public List<Column> Columns
        {
            get { return columns; }
        }

      
        #endregion P R O P E R T I E S

        /// <summary>
        /// Get the Row at this Index
        /// </summary>
        /// <param name="RowIndex"></param>
        /// <returns></returns>
        public Row this[int RowIndex]
        {
            get
            {
                if (RowIndex > rows.Count) return null;
                return rows[RowIndex];
            }
        }

       

        /// <summary>
        /// Add a row to the table
        /// </summary>
        public void AddRow()
        {
            Row newRow = new Row(numCells, this);
            rows.Add(newRow);
            numRows++;
        }

        /// <summary>
        /// Add a number of rows to a Table
        /// </summary>
        /// <param name="NumberOfRows"></param>
        public void AddRows(int NumberOfRows)
        {
             for (int i = 0; i < NumberOfRows; i++)
			{
			    Row newRow = new Row(numCells, this);
                rows.Add(newRow);
                numRows++;
			}
        }

        /// <summary>
        /// Set all the borders of a table to the same value
        /// </summary>
        /// <param name="Border"></param>
        public void SetAllBorders(DocumentFormat.OpenXml.Wordprocessing.BorderValues Border)
        {
            TopBorder = Border;
            BottomBorder = Border;
            LeftBorder = Border;
            RightBorder = Border;
            InsideHorizontalBorder = Border;
            InsideVerticalBorder = Border;
        }


        /// <summary>
        /// Set all the Table borders to the same width
        /// </summary>
        /// <param name="BorderWidth"></param>
        public void SetAllBorderWidths(uint BorderWidth)
        {
            TopBorderWidth = BorderWidth;
            BottomBorderWidth = BorderWidth;
            LeftBorderWidth = BorderWidth;
            RightBorderWidth = BorderWidth;
            InsideHorizontalBorderWidth = BorderWidth;
            InsideVerticalBorderWidth = BorderWidth;
        }

        /// <summary>
        /// Set all the Table borders to the same colours 
        /// </summary>
        /// <param name="BorderColour"></param>
        public void SetAllBorderColours(string BorderColour)
        {
            TopBorderColour = BorderColour;
            BottomBorderColour = BorderColour;
            LeftBorderColour = BorderColour;
            RightBorderColour = BorderColour;
            InsideHorizontalBorderColour = BorderColour;
            InsideVerticalBorderColour = BorderColour;
        }
    }
}
